package com.hl.qzzpbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.qzzpbackend.common.DeleteRequest;
import com.hl.qzzpbackend.exception.BusinessException;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.exception.ThrowUtils;
import com.hl.qzzpbackend.model.dto.resume.ResumeRenameRequest;
import com.hl.qzzpbackend.model.entity.Resume;
import com.hl.qzzpbackend.model.entity.User;
import com.hl.qzzpbackend.model.vo.ResumeVO;
import com.hl.qzzpbackend.service.ResumeService;
import com.hl.qzzpbackend.mapper.ResumeMapper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 简历附件业务层
 * @author helong
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume>
    implements ResumeService{



    /**
     * 简历附件重命名
     * @param resumeRenameRequest 简历附件重命名请求类
     * @param loginUser 登录用户
     * @return 是否成功
     */
    @Override
    public boolean resumeRename(ResumeRenameRequest resumeRenameRequest, User loginUser) {
        Long id = resumeRenameRequest.getId();
        String name = resumeRenameRequest.getName();

        // 参数校验

        Resume oldResume = this.getById(id);
        ThrowUtils.throwIf(oldResume == null, ErrorCode.PARAMS_ERROR, "简历id不存在");
        ThrowUtils.throwIf(!loginUser.getId().equals(oldResume.getUserId()), ErrorCode.PARAMS_ERROR, "简历不属于本用户,不能重命名");
        ThrowUtils.throwIf(name.equals(oldResume.getName()), ErrorCode.PARAMS_ERROR, "简历名称不能相同");

        // 操作数据库，修改简历名称
        Resume resume = new Resume();
        resume.setId(id);
        resume.setName(name);
        boolean result = this.updateById(resume);
        if (!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "重命名简历失败");
        }
        return true;
    }

    /**
     * 获取登录用户简历附件列表
     * @param loginUser 登录用户
     * @return 简历附件列表
     */
    @Override
    public List<ResumeVO> getResumeList(User loginUser) {
        Long userId = loginUser.getId();
        List<Resume> resumeList = this.lambdaQuery().eq(Resume::getUserId, userId).list();
        if(CollUtil.isEmpty(resumeList)){
            return Collections.emptyList();
        }
        return resumeList.stream().map((resume) -> {
            ResumeVO resumeVO = new ResumeVO();
            BeanUtil.copyProperties(resume,resumeVO);
            return resumeVO;
        }).collect(Collectors.toList());
    }

    /**
     * 删除简历附件
     * @param deleteRequest 删除请求类
     * @return 是否成功
     */
    @Override
    public boolean deleteResume(DeleteRequest deleteRequest) {
        Long id = deleteRequest.getId();

        long count = this.lambdaQuery().eq(Resume::getId, id).count();
        ThrowUtils.throwIf(count == 0, ErrorCode.PARAMS_ERROR, "简历id不存在");

        boolean result = this.removeById(id);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "删除简历失败");
        }
        return true;
    }
}




